<template>
  <je-modal
    v-model:visible="visibleCopy"
    width="460px"
    :title="$t('sourceModel.copySource')"
    height="auto"
    :resize="false"
    :maximizable="false"
    :ok-button="saveCopy"
    cancel-button
    @close="cancelCopyModal"
  >
    <je-form layout="vertical" :model="sourceObj">
      <je-form-item
        label-align="top"
        :label="$t('sourceModel.copyOriginSource')"
        name="COPYTABLE_INFO"
      >
        <je-input v-model:value="sourceObj.oldSourceInfo" :disabled="true" />
      </je-form-item>
      <je-form-item
        label-align="top"
        :label="$t('sourceModel.newSourceName')"
        name="name"
        v-bind="validateInfos.name"
      >
        <je-input v-model:value="sourceObj.name" />
      </je-form-item>
      <je-form-item
        label-align="top"
        :label="$t('sourceModel.newSourceCode')"
        name="code"
        v-bind="validateInfos.code"
      >
        <je-input v-model:value="sourceObj.code" />
      </je-form-item>
      <je-form-item
        label-align="top"
        :label="$t('sourceModel.selectModule')"
        name="moduleName"
        v-bind="validateInfos.moduleName"
      >
        <je-tree-select
          v-model:value="sourceObj.moduleName"
          :select-options="{ title: '选择模块' }"
          name="moduleName"
          config-info="JE_CORE_DATASOURCE,moduleName~moduleId,text~id,S"
          :querys="getJQuery()"
          :orders="[{ code: 'SY_ORDERINDEX', type: 'ASC' }]"
        />
      </je-form-item>
    </je-form>
  </je-modal>
</template>

<script>
  import { defineComponent, reactive, toRefs, onMounted } from 'vue';
  import { Input, Modal, Form, InputSelect } from '@jecloud/ui';
  import { verifyData } from '@/helper/common';
  import { copySource } from '@/api/index';
  const useForm = Form.useForm;
  export default defineComponent({
    name: 'ModalCopy',
    components: {
      JeModal: Modal,
      JeForm: Form,
      JeFormItem: Form.Item,
      JeInput: Input,
      JeTreeSelect: InputSelect.Tree,
    },
    props: {
      sourceData: {
        type: Object,
        default: () => ({}),
      },
      productId: {
        type: String,
        default: () => '',
      },
    },
    emits: ['loadSourceTree', 'cancelCopySource'],
    setup(props, { emit }) {
      const state = reactive({
        visibleCopy: true,
        sourceObj: {
          oldSourceInfo: `${props.sourceData.name}(${props.sourceData.code})`,
          name: `${props.sourceData.name}_COPY`,
          code: `${props.sourceData.code}_COPY`,
          moduleName: '', //模块名称
          moduleId: '', //模块id
        },
        rules: {
          name: verifyData.name,
          code: verifyData.code,
          moduleName: [verifyData.required],
        },
      });
      const { resetFields, validate, validateInfos } = useForm(state.sourceObj, state.rules, {
        onValidate: (...args) => {},
      });
      const methods = {
        getJQuery() {
          const j_query = [
            {
              code: 'DATASOURCE_TYPE',
              type: '=',
              value: 'MODULE',
            },
            {
              code: 'SY_PRODUCT_ID',
              type: '=',
              value: props.productId,
            },
          ];

          return j_query;
        },
        //关闭弹窗
        cancelCopyModal() {
          state.visibleCopy = false;
          emit('cancelCopySource');
        },
        //保存复制
        saveCopy({ $modal, button }) {
          const modal = $modal,
            btn = button;
          btn.loading = true;
          validate()
            .then(() => {
              //新增
              const params = {
                tableCode: 'JE_CORE_DATASOURCE', //表编码
                funcCode: 'JE_CORE_DATASOURCE', //功能编码
                targetId: props.sourceData.id, //源id
                DATASOURCE_TEXT: state.sourceObj.name, //名称
                DATASOURCE_CODE: state.sourceObj.code, //编码
                SY_PARENT: state.sourceObj.moduleId, //父id
                SY_PRODUCT_ID: props.productId, //产品ID
              };
              copySource(params).then((res) => {
                if (res.success) {
                  //关闭弹窗
                  btn.loading = false;
                  modal.close();
                  methods.resetModule();
                  Modal.notice('复制成功', 'success');
                  emit('loadSourceTree');
                } else {
                  btn.loading = false;
                  Modal.alert(res.message, 'error');
                }
              });
            })
            .catch((err) => {
              btn.loading = false;
            });
          return false;
        },
        resetModule() {
          resetFields();
          state.sourceObj = {
            oldSourceInfo: '',
            name: '',
            code: '',
            moduleName: '', //模块名称
            moduleId: '', //模块id
          };
        },
      };
      onMounted(() => {});
      return {
        resetFields,
        validateInfos,
        ...toRefs(state),
        ...methods,
      };
    },
  });
</script>
<style scoped lang="less"></style>
